package com.linked.order.shoppingcart.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.order.shoppingcart.bean.po.ShoppingCartInfoPO;
import com.linked.order.shoppingcart.service.IShoppingCartService;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :dbq
 * @date : 2023/5/26 15:12
 * @description : desc
 */
@RestController
@Slf4j
@RequestMapping("shoppingcart")
public class ShoppingCartController {

    private final ObjectMapper mapper;

    private final IShoppingCartService shoppingCartService;

    public ShoppingCartController(ObjectMapper mapper, IShoppingCartService shoppingCartService) {
        this.mapper = mapper;
        this.shoppingCartService = shoppingCartService;
    }

    @PostMapping("addShoppingCard")
    Result addShoppingCard(@RequestBody ShoppingCartInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("添加购物车接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = shoppingCartService.addShoppingCard(param);
        } catch (Exception e) {
            log.error("添加购物车接口 异常！", e);
            return Result.error("未知异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "添加购物车失败！");
    }


}
